package ruide_info_mvc.base.contoller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import ruide_info_mvc.base.core.bean_core.IBeanInvokeCore;
import ruide_info_mvc.base.model.request.BaseRequestBean;
import ruide_info_mvc.base.model.response.BaseResponseBean;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 公共入口
 * @ClassName BaseContoller
 * @Author wangBo
 * @Date 2019/7/20 0020 下午 8:35
 * @Version 1.0
 */
@RestController(value = "baseContoller")
public class BaseContoller {

    private final Logger logger = LoggerFactory.getLogger(BaseContoller.class);

    @Resource(name = "beanInvokeCore")
    private IBeanInvokeCore iBeanInvokeCore;

    @PostMapping(value = "${riode_info_mvc.commonsReqeust}")
    public BaseResponseBean commonsReqeust(BaseRequestBean baseRequestBean , MultipartFile [] files) throws Exception {
        BaseResponseBean baseResponseBean = iBeanInvokeCore.invoke(baseRequestBean , files);
        return baseResponseBean;
    }

    @PostMapping(value = "${riode_info_mvc.commonsDownloadExecl}")
    public void commonsDownloadExecl(BaseRequestBean baseRequestBean) throws Exception {
//        this.createExeclReturnStream();
    }

    /**
     * 导出execl
     *
     * @param fileName    : 文件名称
     * @param sheetlName  : 单元格名称
     * @param nameArray   : 名称数组
     * @param columnArray : 列数组
     * @param paramsList  : 数据
     * @return OutputStream
     * @throws IOException
     */
//    protected OutputStream createExeclReturnStream(String fileName, String sheetlName, String[] nameArray, String[] columnArray, List<Map> paramsList) throws IOException {
//        // index : 迭代行号
//        int index = 0;
//        String columnResult = "";
//        // 创建HSSFWorkbook对象(excel的文档对象)
//        HSSFWorkbook wb = new HSSFWorkbook();
//        //建立新的sheet对象（excel的表单）
//        HSSFSheet sheet = wb.createSheet(sheetlName);
//        //在sheet里创建第一行，参数为行索引(excel的行)，可以是0～65535之间的任何一个
//        HSSFRow row1 = sheet.createRow(index);
//        for (int i = 0; i < nameArray.length; i++) {
//            row1.createCell(i).setCellValue(nameArray[i]);
//        }
//        if (null != paramsList && !paramsList.isEmpty()) {
//            for (Map map : paramsList) {
//                HSSFRow row = sheet.createRow(++index);
//                for (int i = 0; i < columnArray.length; i++) {
//                    //在sheet里创建第三行
//                    if (map.containsKey(columnArray[i])) {
//                        try {
//                            columnResult = map.get(columnArray[i]).toString();
//                        } catch (Exception e) {
//                            columnResult = "";
//                        }
//                    }
//                    row.createCell(i).setCellValue(columnResult);
//                }
//            }
//        }
//        fileName = FileUtils.filenameEncoding(fileName, request);
//        //输出Excel文件
//        OutputStream output = this.getResponse(fileName);
//        wb.write(output);
//        output.close();
//        return output;
//    }
//
//    private OutputStream getResponse(String fileName) {
//        OutputStream output = null;
//        try {
//            output = response.getOutputStream();
//            String agent = request.getHeader("USER-AGENT").toLowerCase();
//            response.reset();
//            response.setContentType("application/vnd.ms-excel");
////        response.setContentType("application/msexcel");           原来的代码
//            try {
//                String codedFileName = URLEncoder.encode(fileName, "UTF-8");
//                if (agent.contains("firefox")) {
//                    response.setCharacterEncoding("utf-8");
//                    response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "ISO8859-1") + ".xls");
//                } else {
//                    response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");
//                }
//            } catch (UnsupportedEncodingException e) {
//                logger.error("", e);
//            }
//        } catch (IOException e) {
//            logger.error("", e);
//        }
//        return output;
//    }
}